package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcFk;
import com.ruoyi.gcgl.domain.GcFkfp;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectFlow;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtFj;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcFkService;
import com.ruoyi.gcgl.service.IGcFkfpService;
import com.ruoyi.gcgl.service.IGcProjectFlowService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtFjService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.tool.AliyunSmsUtils;

/**
 * 施工安装合同Controller
 * 
 * @author ruoyi
 * @date 2022-11-29
 */
@Controller
@RequestMapping("/gcgl/sgazht")
public class GcProjectSgazhtController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(GcProjectQtwjController.class);
	
    private String prefix = "gcgl/sgazht";

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcProjectFlowService gcProjectFlowService;
    @Autowired
    private IGcProjectSgazhtFjService gcProjectSgazhtFjService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IGcSupplyService gcSupplyService;
    
    private static final String FILE_DELIMETER = ",";
    
    @Autowired
    private IGcFkfpService gcFkfpService;
    @Autowired
    private IGcFkService gcFkService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;

    @RequiresPermissions("gcgl:sgazht:view")
    @GetMapping()
    public String sgazht()
    {
        return prefix + "/sgazht";
    }

    /**
     * 查询施工安装合同列表
     */
    @RequiresPermissions("gcgl:sgazht:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSgazht gcProjectSgazht)
    {
        startPage();
        gcProjectSgazht.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcProjectSgazht.setCreateUser(getLoginName());
        }
        List<GcProjectSgazht> list = gcProjectSgazhtService.selectGcProjectSgazhtList(gcProjectSgazht);
        return getDataTable(list);
    }

    /**
     * 导出施工安装合同列表
     */
    @RequiresPermissions("gcgl:sgazht:export")
    @Log(title = "施工安装合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSgazht gcProjectSgazht)
    {
        List<GcProjectSgazht> list = gcProjectSgazhtService.selectGcProjectSgazhtList(gcProjectSgazht);
        ExcelUtil<GcProjectSgazht> util = new ExcelUtil<GcProjectSgazht>(GcProjectSgazht.class);
        return util.exportExcel(list, "施工安装合同数据");
    }

    /**
     * 新增施工安装合同
     */
    @GetMapping("/add/{gcProjectid}")
    public String add(ModelMap mmap,@PathVariable("gcProjectid") Long gcProjectid){
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectid);
    	mmap.put("gcProject", gcProject);
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	String[] typelis = new String[]{"3","4"};
    	gcSupply.setTypelis(typelis);
    	List<GcSupply> gcSupplyLis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplyLis", gcSupplyLis);
        return prefix + "/add";
    }

    /**
     * 新增保存施工安装合同
     */
    @RequiresPermissions("gcgl:sgazht:add")
    @Log(title = "施工安装合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcProjectSgazht gcProjectSgazht){
    	try {
//    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
//    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
//    		int year = Integer.parseInt(sdy.format(new Date()));
//    		int month = Integer.parseInt(sdm.format(new Date()));
//    		GcProjectSgazht newObj = new GcProjectSgazht();
//    		newObj.setDeleteFlag(1);
//    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
//    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
//    		List<GcProjectSgazht> lisCount = gcProjectSgazhtService.selectGcProjectSgazhtList(newObj);
//    		String no = String.format("%02d", lisCount.size()+1);
    		
    		GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectSgazht.getGcProjectid());
    		gcProjectSgazht.setOrderNo(gcProject.getOrderNo());
    		//施工安装合同
    		GcSupply supplyObj = gcSupplyService.selectGcSupplyById(gcProjectSgazht.getSupplyId());
    		gcProjectSgazht.setSupplyName(supplyObj.getName());
        	gcProjectSgazht.setStatus(0);
        	gcProjectSgazht.setDeleteFlag(1);
        	gcProjectSgazht.setCreateAt(new Date());
        	gcProjectSgazht.setUpdateAt(new Date());
        	gcProjectSgazht.setCreateUser(getLoginName());
        	gcProjectSgazht.setUpdateUser(getLoginName());
        	SysUser user = sysUserService.selectUserById(getUserId());
        	gcProjectSgazht.setSpr(user.getZjsjName());//2024年12月30日修改，增加分管领导审批
        	
        	gcProjectSgazhtService.insertGcProjectSgazht(gcProjectSgazht);
        	
        	//查询流程图
            GcProjectFlow gcProjectFlow = new GcProjectFlow();
            gcProjectFlow.setGcProjectid(gcProjectSgazht.getGcProjectid());
            gcProjectFlow = gcProjectFlowService.selectGcProjectFlowList(gcProjectFlow).get(0);
            
            GcProjectFlow obj = new GcProjectFlow();
            //判断是否启用项目审批节点
            obj.setShazht(1);
            obj.setShazhtps(1);
            obj.setShazhtfp(0);
            obj.setShazhtfk(0);
            obj.setShazhtgc(0);
            obj.setShazhtjgcl(0);
            obj.setShazhtjgys(0);
            obj.setGcProjectid(gcProjectFlow.getGcProjectid());
            gcProjectFlowService.updateGcProjectFlow(obj);
            
          //发送消息通知
//        	gcSendTipService.sendTipXm("sgazht", gcProjectSgazht.getName(), gcProjectSgazht.getOrderNo(), 
//        			gcProjectSgazht.getCreateUser(), DateUtils.dateTime(gcProjectSgazht.getCreateAt()), 
//        			gcProjectSgazht.getStatus().toString(), null, gcProjectSgazht.getId().toString());
            
            try {
            	SysUser user1 = sysUserService.selectUserById(user.getZjsjId());
    			String phonenumber = user1.getPhonenumber();//分管领导
    			AliyunSmsUtils.returnSmsTz(phonenumber);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 修改施工安装合同
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(id);
        mmap.put("zlwjvo", gcProjectSgazht.getZlwj());
        if(gcProjectSgazht.getZlwj()!=null && !gcProjectSgazht.getZlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getZlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setZlwj(file);
        }
        
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	String[] typelis = new String[]{"3","4"};
    	gcSupply.setTypelis(typelis);
    	List<GcSupply> gcSupplyLis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplyLis", gcSupplyLis);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工安装合同
     */
    @Log(title = "施工安装合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSgazht gcProjectSgazht){
    	try {
    		GcSupply supplyObj = gcSupplyService.selectGcSupplyById(gcProjectSgazht.getSupplyId());
    		gcProjectSgazht.setSupplyName(supplyObj.getName());
    		gcProjectSgazht.setStatus(0);
    		gcProjectSgazht.setSpr("");
    		gcProjectSgazht.setZjl("");
    		gcProjectSgazht.setUpdateAt(new Date());
    		gcProjectSgazht.setUpdateUser(getLoginName());
    		gcProjectSgazhtService.updateGcProjectSgazht(gcProjectSgazht);
		} catch (Exception e) {
            return error(e.getMessage());
		}
    	
        return toAjax(1);
    }

    /**
     * 删除施工安装合同
     */
    @RequiresPermissions("gcgl:sgazht:remove")
    @Log(title = "施工安装合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSgazhtService.deleteGcProjectSgazhtByIds(ids));
    }
    
    //项目评审
    @RequiresPermissions("gcgl:sgazht:psview")
    @GetMapping("/ps")
    public String sgazhtps(){
        return prefix + "/sgazhtps";
    }
    
    /**
     * 查询施工安装合同列表
     */
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo pslist(GcProjectSgazht gcProjectSgazht)
    {
        startPage();
        gcProjectSgazht.setDeleteFlag(1);
        gcProjectSgazht.setStatus(0);
        gcProjectSgazht.setSpr(getLoginName());
        List<GcProjectSgazht> list = gcProjectSgazhtService.selectGcProjectSgazhtList(gcProjectSgazht);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSgazht sgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i].toString()));
    		if(sgazht!=null){
    			GcProjectSgazht gcProjectSgazht = new GcProjectSgazht();
        		gcProjectSgazht.setId(Long.parseLong(id[i]));
        		gcProjectSgazht.setStatus(1);
        		gcProjectSgazht.setSpr(getLoginName());
        		gcProjectSgazht.setSprAt(new Date());
        		gcProjectSgazht.setSpyj(spyj);
        		gcProjectSgazhtService.updateGcProjectSgazht(gcProjectSgazht);
        		
        		GcProjectSgazht l_obj = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i]));
        		//发送消息通知
//            	gcSendTipService.sendTipXm("sgazht", l_obj.getName(), l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
        		
        		try {
//            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
//            		if(userlis!=null && userlis.size()>0){
//            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
        			String phonenumber = zjl;
            			AliyunSmsUtils.returnSmsTz(phonenumber);
//            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSgazht sgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i].toString()));
    		if(sgazht!=null){
    			GcProjectSgazht gcProjectSgazht = new GcProjectSgazht();
        		gcProjectSgazht.setId(Long.parseLong(id[i]));
        		gcProjectSgazht.setStatus(2);
        		gcProjectSgazht.setSpr(getLoginName());
        		gcProjectSgazht.setSprAt(new Date());
        		gcProjectSgazht.setSpyj(spyj);
        		gcProjectSgazhtService.updateGcProjectSgazht(gcProjectSgazht);
        		
        		GcProjectFlow obj = new GcProjectFlow();
            	obj.setGcProjectid(Long.parseLong(id[i]));
                obj.setShazhtps(0);
                obj.setUpdateAt(new Date());
                obj.setUpdateUser(getLoginName());
            	gcProjectFlowService.updateGcProjectFlow(obj);
            	
            	GcProjectSgazht l_obj = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i]));
        		//发送消息通知
//            	gcSendTipService.sendTipXm("sgazht", l_obj.getName(), l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
            	
            	try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	return toAjax(1);
    }
    
    
  //项目评审
    @RequiresPermissions("gcgl:sgazht:zjlpsview")
    @GetMapping("/zjlps")
    public String sgazhtzjlps(){
        return prefix + "/sgazhtzjlps";
    }
    
    /**
     * 查询施工安装合同列表
     */
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcProjectSgazht gcProjectSgazht)
    {
        startPage();
        gcProjectSgazht.setDeleteFlag(1);
        gcProjectSgazht.setStatus(1);
        List<GcProjectSgazht> list = gcProjectSgazhtService.selectGcProjectSgazhtList(gcProjectSgazht);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSgazht sgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i].toString()));
    		if(sgazht!=null){
    			GcProjectSgazht gcProjectSgazht = new GcProjectSgazht();
        		gcProjectSgazht.setId(Long.parseLong(id[i]));
        		gcProjectSgazht.setStatus(3);
        		gcProjectSgazht.setZjl(getLoginName());
        		gcProjectSgazht.setZjlAt(new Date());
        		gcProjectSgazht.setSpyj(spyj);
        		gcProjectSgazhtService.updateGcProjectSgazht(gcProjectSgazht);
        		
        		GcProjectSgazht l_obj = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i]));
        		//发送消息通知
//            	gcSendTipService.sendTipXm("sgazht", l_obj.getName(), l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
        		
        		try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectSgazht sgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i].toString()));
    		if(sgazht!=null){
    			GcProjectSgazht gcProjectSgazht = new GcProjectSgazht();
        		gcProjectSgazht.setId(Long.parseLong(id[i]));
        		gcProjectSgazht.setStatus(4);
        		gcProjectSgazht.setZjl(getLoginName());
        		gcProjectSgazht.setZjlAt(new Date());
        		gcProjectSgazht.setSpyj(spyj);
        		gcProjectSgazhtService.updateGcProjectSgazht(gcProjectSgazht);
            	
            	GcProjectFlow obj = new GcProjectFlow();
            	obj.setGcProjectid(Long.parseLong(id[i]));
                obj.setShazhtps(0);
                obj.setUpdateAt(new Date());
                obj.setUpdateUser(getLoginName());
            	gcProjectFlowService.updateGcProjectFlow(obj);
            	
            	GcProjectSgazht l_obj = gcProjectSgazhtService.selectGcProjectSgazhtById(Long.parseLong(id[i]));
        		//发送消息通知
//            	gcSendTipService.sendTipXm("sgazht", l_obj.getName(), l_obj.getOrderNo(), 
//            			l_obj.getCreateUser(), DateUtils.dateTime(l_obj.getCreateAt()), 
//            			l_obj.getStatus().toString(), null, l_obj.getId().toString());
            	
            	try {
            		List<Map> userlis = gcSendTipMapper.selectGcToUserByName(l_obj.getCreateUser());
            		if(userlis!=null && userlis.size()>0){
            			String phonenumber = userlis.get(0).get("phonenumber").toString();//创建人
            			AliyunSmsUtils.returnSmsTz(phonenumber);
            		}
    			} catch (Exception e) {
    				// TODO: handle exception
    				e.printStackTrace();
    			}
    		}
    	}
    	return toAjax(1);
    }
    
    /**
     * 修改施工安装合同
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(id);
        if(gcProjectSgazht.getZlwj()!=null && !gcProjectSgazht.getZlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getZlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setZlwj(file);
        }
        
        if(gcProjectSgazht.getHzzszlwj()!=null && !gcProjectSgazht.getHzzszlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getHzzszlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setHzzszlwj(file);
        }
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
        gcProjectSgazhtFj.setDeleteFlag(1);
        gcProjectSgazhtFj.setSgazhtId(id);
        List<GcProjectSgazhtFj> sgazhtFjlis = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        
        List<Map> htlis = new ArrayList<Map>();
    	List<Map> fjlis = new ArrayList<Map>();
    	for(GcProjectSgazhtFj fj:sgazhtFjlis){
    		if(fj.getType().equals("1")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else if(fj.getType().equals("3")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else{
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			fjlis.add(rep);
    		}
    	}
    	
    	mmap.put("htlis", htlis);
    	mmap.put("fjlis", fjlis);
        
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	String[] typelis = new String[]{"3","4"};
    	gcSupply.setTypelis(typelis);
    	List<GcSupply> gcSupplyLis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplyLis", gcSupplyLis);
    	
    	//付款列表
    	double fkjehz = 0.0;//发票金额汇总
    	GcFk gcFk = new GcFk();
    	gcFk.setDeleteFlag(1);
    	gcFk.setGcProjectid(gcProjectSgazht.getGcProjectid());
    	gcFk.setProjectId(gcProjectSgazht.getId());
    	//1.设备材料合同 2.施工安装合同 3.工程其它合同 4.一般性费用 5.其它费用
    	gcFk.setXmtype(2);
    	gcFk.setCkdw(gcProjectSgazht.getSupplyName());
    	List<GcFk> fklis = gcFkService.selectGcFkList(gcFk);
    	for(GcFk obj:fklis){
    		fkjehz = DecimalCalculateUtil.add(fkjehz, Double.valueOf(obj.getLjzf().toString()));
    	}
    	mmap.put("fklis", fklis);
    	mmap.put("fkjehz", fkjehz);
    	
    	//发票列表
    	double fpjehz = 0.0;//发票金额汇总
    	String fpFile = "";
    	GcFkfp gcFkfp = new GcFkfp();
    	gcFkfp.setDeleteFlag(1);
    	gcFkfp.setGcProjectid(gcProjectSgazht.getGcProjectid());
    	gcFkfp.setProjectId(gcProjectSgazht.getId());
    	//1.设备材料合同 2.施工安装合同 3.工程其它合同 4.一般性费用 5.其它费用
    	gcFkfp.setXmtype(2);
    	gcFkfp.setCkdw(gcProjectSgazht.getSupplyName());
    	List<GcFkfp> fkfplis = gcFkfpService.selectGcFkfpList(gcFkfp);
    	for(GcFkfp obj:fkfplis){
    		fpjehz = DecimalCalculateUtil.add(fpjehz, Double.valueOf(obj.getFpjine().toString()));
    		fpFile = "";
    		String[] zlis = obj.getFap().split(",");
			for(int i=0;i<zlis.length;i++){
				if(zlis[i].equals("")){
					fpFile = "";
				}else{
					fpFile += RuoYiConfig.getOnPath()+zlis[i];
				}
        	}
			obj.setFap(fpFile);
			obj.setFpriqvo(DateUtils.dateTime(obj.getFpriq()));
    	}
    	mmap.put("fkfplis", fkfplis);
    	mmap.put("fpjehz", fpjehz);
        return prefix + "/detail";
    }
    
    /**
     * 修改施工安装合同
     */
    @GetMapping("/detailgcsp/{id}")
    public String detailgcsp(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(id);
        if(gcProjectSgazht.getZlwj()!=null && !gcProjectSgazht.getZlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getZlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setZlwj(file);
        }
        
        if(gcProjectSgazht.getHzzszlwj()!=null && !gcProjectSgazht.getHzzszlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getHzzszlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setHzzszlwj(file);
        }
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
        gcProjectSgazhtFj.setDeleteFlag(1);
        gcProjectSgazhtFj.setSgazhtId(id);
        List<GcProjectSgazhtFj> sgazhtFjlis = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        
        List<Map> htlis = new ArrayList<Map>();
    	List<Map> fjlis = new ArrayList<Map>();
    	for(GcProjectSgazhtFj fj:sgazhtFjlis){
    		if(fj.getType().equals("1")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else if(fj.getType().equals("3")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else{
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			fjlis.add(rep);
    		}
    	}
    	
    	mmap.put("htlis", htlis);
    	mmap.put("fjlis", fjlis);
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	String[] typelis = new String[]{"3","4"};
    	gcSupply.setTypelis(typelis);
    	List<GcSupply> gcSupplyLis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplyLis", gcSupplyLis);
        
        return prefix + "/detailgcsp";
    }
    
    /**
     * 修改施工安装合同
     */
    @GetMapping("/detailzjlsp/{id}")
    public String detailzjlsp(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(id);
        if(gcProjectSgazht.getZlwj()!=null && !gcProjectSgazht.getZlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getZlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setZlwj(file);
        }
        
        if(gcProjectSgazht.getHzzszlwj()!=null && !gcProjectSgazht.getHzzszlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getHzzszlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setHzzszlwj(file);
        }
        mmap.put("gcProjectSgazht", gcProjectSgazht);
        
        GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
        gcProjectSgazhtFj.setDeleteFlag(1);
        gcProjectSgazhtFj.setSgazhtId(id);
        List<GcProjectSgazhtFj> sgazhtFjlis = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        
        List<Map> htlis = new ArrayList<Map>();
    	List<Map> fjlis = new ArrayList<Map>();
    	for(GcProjectSgazhtFj fj:sgazhtFjlis){
    		if(fj.getType().equals("1")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else if(fj.getType().equals("3")){
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			htlis.add(rep);
    		}else{
    			Map rep = new HashMap();
    			if(fj.getZilwj()!=null && !fj.getZilwj().equals("")){
    				rep.put("zilwj", RuoYiConfig.getOnPath()+fj.getZilwj());
    			}else{
    				rep.put("zilwj", "");
    			}
    			fjlis.add(rep);
    		}
    	}
    	
    	mmap.put("htlis", htlis);
    	mmap.put("fjlis", fjlis);
        
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	String[] typelis = new String[]{"3","4"};
    	gcSupply.setTypelis(typelis);
    	List<GcSupply> gcSupplyLis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplyLis", gcSupplyLis);
    	
        return prefix + "/detailzjlsp";
    }
    
    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception{
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
    
    //---------上传正式合同
    @GetMapping("/editZsht/{id}")
    public String editZsht(@PathVariable("id") Long id, ModelMap mmap){
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(id);
    	
    	GcProjectSgazhtFj sgazhtFj = new GcProjectSgazhtFj();
    	sgazhtFj.setDeleteFlag(1);
    	sgazhtFj.setType("3");
    	sgazhtFj.setSgazhtId(id);
    	List<GcProjectSgazhtFj> fj = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(sgazhtFj);
    	
    	if(fj!=null && fj.size()>0){
    		gcProjectSgazht.setZlwj(fj.get(0).getZilwj());
    	}else{
    		gcProjectSgazht.setZlwj(null);
    	}
    	
    	if(gcProjectSgazht.getZlwj()!=null && !gcProjectSgazht.getZlwj().equals("")){
        	String[] zlis = gcProjectSgazht.getZlwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += RuoYiConfig.getOnPath()+zlis[i]+",";
        	}
        	gcProjectSgazht.setZlwj(file);
        }
    	
    	mmap.put("gcProjectSgazht", gcProjectSgazht);
        return prefix + "/editZsht";
    }

    /**
     * 修改上传正式合同
     */
    @Log(title = "上传施工安装正式合同", businessType = BusinessType.UPDATE)
    @PostMapping("/editZsht")
    @ResponseBody
    public AjaxResult editZshtSave(GcProjectSgazht gcProjectSgazht){
    	try {
    		GcProjectSgazht sgazht = new GcProjectSgazht(); 
    		sgazht.setStatus(6);
    		sgazht.setUpdateUser(getLoginName());
    		sgazht.setUpdateAt(new Date());
    		sgazht.setId(gcProjectSgazht.getId());
    		gcProjectSgazhtService.updateGcProjectSgazht(sgazht);
    		
    		GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
    		gcProjectSgazhtFj.setDeleteFlag(1);
    		gcProjectSgazhtFj.setGcProjectid(gcProjectSgazht.getGcProjectid());
    		gcProjectSgazhtFj.setOrderNo(gcProjectSgazht.getOrderNo());
    		gcProjectSgazhtFj.setSgazhtId(gcProjectSgazht.getId());
    		gcProjectSgazhtFj.setType("3");//1合同，2询价单，3正式合同
        	List<GcProjectSgazhtFj> lis = gcProjectSgazhtFjService.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
        	
        	if(lis!=null && lis.size()>0){
        		GcProjectSgazhtFj obj = new GcProjectSgazhtFj();
        		obj.setSgazhtId(gcProjectSgazht.getId());
            	obj.setZilwj(gcProjectSgazht.getZlwj());
            	obj.setType("3");//1合同，2询价单，3正式合同
            	gcProjectSgazhtFjService.updateGcProjectSgazhtFj4type(obj);
        	}else{
        		GcProjectSgazhtFj obj = new GcProjectSgazhtFj();
        		obj.setGcProjectid(gcProjectSgazht.getGcProjectid());
        		obj.setOrderNo(gcProjectSgazht.getOrderNo());
        		obj.setSgazhtId(gcProjectSgazht.getId());
        		obj.setType("3");//1合同，2询价单，3正式合同
        		obj.setCreateUser(getLoginName());
        		obj.setUpdateUser(getLoginName());
        		obj.setCreateAt(new Date());
        		obj.setUpdateAt(new Date());
        		obj.setDeleteFlag(1);
        		obj.setZilwj(gcProjectSgazht.getZlwj());
            	gcProjectSgazhtFjService.insertGcProjectSgazhtFj(obj);
        	}
    		
    		//上传项目正式合同-修改流程节点
        	GcProjectFlow obj = new GcProjectFlow();
        	obj.setGcProjectid(gcProjectSgazht.getGcProjectid());
            obj.setShazhtps(1);
            obj.setShazhtfp(1);
            obj.setShazhtfk(1);
            obj.setShazhtgc(1);
            obj.setShazhtjgys(1);
            obj.setUpdateAt(new Date());
            obj.setUpdateUser(getLoginName());
        	gcProjectFlowService.updateGcProjectFlow(obj);
		} catch (Exception e) {
			// TODO: handle exception
			return error(e.getMessage());
		}
        return toAjax(1);
    }
}